home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr26 / netprog.zip / NETPROG.TAR / net.s5 / acceptcall.c next >
C/C++ Source or Header  |  1989-12-17  |  2KB  |  79 lines

  1. /*
  2.  * Accept an incoming connection request.
  3.  *
  4.  * Return the new descriptor that refers to the newly accepted connection,
  5.  * or -1.  The only time we return -1 is if a disconnect arrives before
  6.  * the accept is performed.
  7.  */
  8.  
  9. #include    <stdio.h>
  10. #include    <tiuser.h>
  11. #include    <fcntl.h>
  12. #include    <stropts.h>
  13.  
  14. int
  15. accept_call(listenfd, callptr, name, rwflag)
  16. int        listenfd;    /* the descriptor caller used for t_listen() */
  17. struct t_call    *callptr;    /* from t_listen(), passed to t_accept() */
  18. char        *name;        /* name of transport provider */
  19. int        rwflag;        /* if nonzero, push read/write module */
  20. {
  21.     int        newfd;
  22.     extern int    t_errno;
  23.  
  24.     /*
  25.      * Open the transport provider to get a new file descriptor.
  26.      */
  27.  
  28.     if ( (newfd = t_open(name, O_RDWR, (struct t_info *) 0)) < 0)
  29.         err_sys("t_open error");
  30.  
  31.     /*
  32.      * Bind any local address to the new descriptor.  Since this
  33.      * function is intended to be called by a server after a
  34.      * connection request has arrived, any local address will suffice.
  35.      */
  36.  
  37.     if (t_bind(newfd, (struct t_bind *) 0, (struct t_bind *) 0) < 0)
  38.         err_sys("t_bind error");
  39.  
  40.     /*
  41.      * Accept the connection request on the new descriptor.
  42.      */
  43.  
  44.     if (t_accept(listenfd, newfd, callptr) < 0) {
  45.         if (t_errno == TLOOK) {
  46.             /*
  47.              * An asynchronous event has occurred.  We must have
  48.              * received a disconnect.  Go ahead and call t_rcvdis(),
  49.              * then close the new file descriptor that we opened
  50.              * above.
  51.              */
  52.  
  53.             if (t_rcvdis(listenfd, (struct t_discon *) 0) < 0)
  54.                 err_sys("t_rcvdis error");
  55.             if (t_close(newfd) < 0)
  56.                 err_sys("t_close error");
  57.             return(-1);    /* return error to caller */
  58.         }
  59.         err_sys("t_accept error");
  60.     }
  61.  
  62.     /*
  63.      * If the caller requests, push the streams module "tirdwr" onto
  64.      * the new stream, so that the read(2) and write(2) system calls
  65.      * can be used.  We first have to pop the "timod" module (the
  66.      * default).
  67.      */
  68.  
  69.     if (rwflag) {
  70.         if (ioctl(newfd, I_POP, (char *) 0) < 0)
  71.             err_dump("I_POP of timod failed");
  72.  
  73.         if (ioctl(newfd, I_PUSH, "tirdwr") < 0)
  74.             err_dump("I_PUSH of tirdwr failed");
  75.     }
  76.  
  77.     return(newfd);
  78. }
  79.